import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:banquanjie_app/app/services/HttpsRequest.dart';
import 'package:banquanjie_app/app/widget/common/dropdownMenu.dart';

class CompanyListController extends GetxController {
  //TODO: Implement CompanyListController
  final DropDownMenuController dropDownMenuController = DropDownMenuController();
  final HttpsClient httpsClient = HttpsClient();
  final RxMap<String, dynamic> query = <String, dynamic>{
    "brandType": "",
    "initial": "",
    "occupationIds": "",
    "pageCode": 1,
    "pageSize": 9,
    "userName": ""
  }.obs;
  final RxMap<String, dynamic> tempQuery = <String, dynamic>{}.obs;
  final RxList companyList = [].obs;
  bool flag = false;
  final ScrollController scrollController = ScrollController();
  final RxInt total = RxInt(-1);
  final RxMap dicList = {
    'brandTypeList': [],
    'initialList': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  }.obs;
  @override
  void onInit() {
    super.onInit();
    tempQuery.value = Map<String, dynamic>.from(query);
    fnGet();
    getQueryList('brandTypeList', { "dicType": "ent_big_type" });
    scrollController.addListener(() {
      if (scrollController.position.pixels == scrollController.position.maxScrollExtent - 100 && !flag && total.value > companyList.length) {
        flag = true;
        query['pageCode']++;
        update();
        fnGet();
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
  }
  void getQueryList(key, query) async {
    var res = await httpsClient.post('/user/dic/list',  query);
    var result = res.data;
    if (result['code'] == 200) {
      dicList[key] = result['data'].map((item) => ({ "id": item['id'], "dicName": item['dicName']})).toList();
      dicList[key].insert(0, { "id": '', "dicName": '全部'});
      update();
    }
  }
  // /web/user/up/brandList
  void fnGet() async{
    var res = await httpsClient.post('/user/up/brandList', query);
     var result = res.data;
    if (result['code'] == 200) {
      total.value = result['data']['total'];
      companyList.addAll(result['data']['rows']);
      flag = false;
      update();
    }
  }
  setSearchQueryValue(key, value) {
    if (tempQuery[key] == value) {
      tempQuery[key] = '';
    } else {
      tempQuery[key] = value;
    }
    update();
    // projectList.value = [];
    // fnGet();
  }
  onSearch() {
    // query.assignAll(tempQuery);
    query.value = Map<String, dynamic>.from(tempQuery);
    companyList.value = [];
    query['pageCode'] = 1;
    fnGet();
    dropDownMenuController.close();
  }
  closeSearchMask() {
    tempQuery.value = Map<String, dynamic>.from(query);
  }
  onClearCondition(key) {
    query[key] = '';
    companyList.value = [];
    fnGet();
    dropDownMenuController.close();
  }
  Future<void> onRefresh() async{
    companyList.value = [];
    query['pageCode'] = 1;
    update();
    fnGet();
  }
}
